package sample.util;

import java.sql.*;

/**
 * @Description: 内嵌数据库H2的使用工具类
 * @author：wutc
 * @time: 2021年4月22日
 */

public class H2dbCRUDUtil {


    /**
     * 以嵌入式(本地)连接方式连接H2数据库
     */
    private String JDBC_URL = "jdbc:h2:/Users/wutongchun/IdeaProjects/h2Demo/database/test";

    /**
     * 使用TCP/IP的服务器模式(远程连接)方式连接H2数据库(推荐)
     */
    // private static final String JDBC_URL = "jdbc:h2:tcp://10.35.14.122/C:/H2/user";

    private String USER = "root";
    private String PASSWORD = "root";
    private String DRIVER_CLASS = "org.h2.Driver";

    private Connection connection = null;
    private Statement statement;


    public H2dbCRUDUtil(String JDBC_URL) {
        this.JDBC_URL = JDBC_URL;
    }

    public String getJDBC_URL() {
        return JDBC_URL;
    }

    public void setJDBC_URL(String JDBC_URL) {
        this.JDBC_URL = JDBC_URL;
    }

    public String getUSER() {
        return USER;
    }

    public void setUSER(String USER) {
        this.USER = USER;
    }

    public String getPASSWORD() {
        return PASSWORD;
    }

    public void setPASSWORD(String PASSWORD) {
        this.PASSWORD = PASSWORD;
    }

    public String getDRIVER_CLASS() {
        return DRIVER_CLASS;
    }

    public void setDRIVER_CLASS(String DRIVER_CLASS) {
        this.DRIVER_CLASS = DRIVER_CLASS;
    }

    public Connection getConnection() {
        return connection;
    }

    //连接
    public void connect(){
        try {
            Class.forName(DRIVER_CLASS);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("读取 DRIVER_CLASS 异常");
        }
        try {
            this.connection = DriverManager.getConnection(JDBC_URL, USER, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("获取本地数据库连接异常");
        }
        try {
            this.statement = connection.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("获取Statement 异常");
        }
    }

    public void disConnect(){
        try {
            statement.close();
            this.connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("断开h2 本地数据库连接异常");
        }
    }

    //增删改查
    public ResultSet createSql(String sql){
        ResultSet resultSet = null;
        try {
            statement.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("CreateSql sql[ "+sql+" ]执行异常");
        }finally {
            return resultSet;
        }
    }

    public int insertOrUpdateSql(String sql){
        int resultSet = 0;
        try {
            resultSet = this.statement.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("insertOrUpdateSql sql[ "+sql+" ]执行异常");
        }finally {
            return resultSet;
        }

    }

    public ResultSet selectSql(String sql){
        ResultSet resultSet = null;
        try {
            resultSet = this.statement.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("selectSql_sql[ "+sql+" ]执行异常");
        }finally {
            return resultSet;
        }

    }



}
